﻿using CRM.Admin.Contracts.Contract.Customer;
using CRM.Admin.Contracts.Contract.Logs;
using CRM.Admin.Contracts.Contract.Sys;
using CRM.Admin.Contracts.Dto.Customer;
using CRM.Admin.Contracts.Dto.Logs;
using CRM.Admin.Contracts.Dto.Sys;
using CRM.Admin.Contracts.Model.Customer;
using CRM.Admin.Contracts.Model.Sys;
using KingsSharp.Common;
using KingsSharp.Common.Extensions;
using KingsSharp.Common.Filter;
using KingsSharp.Core.Data.Entity;
using KingsSharp.Core.Data.Extensions;
using KingsSharp.Web.Helper;
using KingsSharp.Web.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace CRM.Admin.Areas.Customer.Controllers
{
    public class ServeMgController : Controller
    {
        public const string CUSTOMER_FILEPATH = "BaseCustomerfiles//";

        public class DicCatalogType
        {
            /// <summary>
            /// 服务分类
            /// </summary>
            public const string FWFL = "FWFL";
        }
        /// <summary>
        /// 服务管理情况业务契约
        /// </summary>
        public IServeMgService ServeMgService { get; set; }

        public IDictionaryService DictionaryService { get; set; }

        #region 视图功能
        /// <summary>
        /// 列表页
        /// </summary>
        /// <returns></returns>
        public ActionResult ServeMgIndex()
        {
            return View();
        }

        /// <summary>
        /// 新增页
        /// </summary>
        /// <returns></returns>
        public ActionResult ServeMgAdd(Guid CustomerId)
        {
            ViewBag.CustomerId = CustomerId;
            var FWFLdtos = DictionaryService.ItemDictionary.
                 Where(a => a.Parent.Code == DicCatalogType.FWFL).ToList().MapToList<Dictionary, DictionaryDto>();
            ViewData[DicCatalogType.FWFL] = FWFLdtos;
            return View();
        }
        /// <summary>
        /// 编辑页
        /// </summary>
        /// <param name="ServeMgId"></param>
        /// <returns></returns>
        public ActionResult ServeMgEdit(Guid ServeMgId)
        {
            var FWFLdtos = DictionaryService.ItemDictionary.
                 Where(a => a.Parent.Code == DicCatalogType.FWFL).ToList().MapToList<Dictionary, DictionaryDto>();
            ViewData[DicCatalogType.FWFL] = FWFLdtos;
            var dto = ServeMgService.Item.Where(a => a.Id == ServeMgId).FirstOrDefault().MapTo<ServeMgDto>();
            return View(dto);
        }

        #endregion

        #region 数据功能
        public ActionResult ServeMgInsert(ServeMgDto dto)
        {
            dto.CheckNotNull(nameof(dto));
            dto.Id = Guid.NewGuid();
            var result = ServeMgService.Insert(dto);
            string msg = result.Message ?? result.ResultType.ToDescription();
            AjaxResultType msgType = result.ResultType.ToAjaxResultType();
            if (msgType != AjaxResultType.Error)
            {
                msg = "服务管理记录添加成功。";
            }
            return Json(new AjaxResult(msg, msgType));
        }
        public ActionResult ServeMgUpdate(ServeMgDto dto)
        {
            dto.CheckNotNull(nameof(dto));
            var result = ServeMgService.Update(dto);
            string msg = result.Message ?? result.ResultType.ToDescription();
            AjaxResultType msgType = result.ResultType.ToAjaxResultType();
            if (msgType != AjaxResultType.Error)
            {
                msg = "服务管理记录更新成功。";
            }
            return Json(new AjaxResult(msg, msgType));
        }
        public ActionResult ServeMgDelete(Guid UserId)
        {
            var result = ServeMgService.Delete(UserId);
            string msg = result.Message ?? result.ResultType.ToDescription();
            AjaxResultType msgType = result.ResultType.ToAjaxResultType();
            if (msgType != AjaxResultType.Error)
            {
                msg = "删除成功。";
            }
            return Json(new AjaxResult(msg, msgType));
        }
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="dtos"></param>
        /// <returns></returns>
        public ActionResult GetServeMgGridData()
        {
            GridRequest request = new GridRequest(Request);
            Expression<Func<ServeMg, bool>> predicate = FilterHelper.GetExpression<ServeMg>(request.FilterGroup);
            int total;
            var data = ServeMgService.Item.OrderBy(o => o.Order)
                .Where(a => a.IsDeleted == false)
                .Where<ServeMg, Guid>(predicate, request.PageCondition, out total)
                .Select(s => new
                {
                    s.Id,
                    s.Type,
                    s.CustomerId,
                    s.CustomPerson,
                    s.CustomPersonPath,
                    s.StartDate,
                    s.EndDate,
                    s.PgPerson,
                    s.PgPersonPath,
                    s.Report,
                    s.ReportPath,
                    s.CreatedTime
                }).ToList();
            return Json(new GridData<object>(data, total), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 图片、附件上传
        /// </summary>
        /// <returns></returns>
        public ActionResult UploaderProcess()
        {
            UploaderResult result = null;
            try
            {
                string path = CUSTOMER_FILEPATH;
                result = new WebUploader().Process(Request, path, file => Guid.NewGuid().ToString() + "_" + file.FileName);
            }
            catch (Exception ex)
            {
                result = new UploaderResult { Message = ex.Message };
                throw new Exception(ex.Message);
            }
            return Json(result);
        }

        #endregion
    }
}